package com.ronghui.cash.interceptor;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.ronghui.cash.entity.AuthReject;
import com.ronghui.cash.service.AuthRejectService;
import com.ronghui.cash.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class CheckAuthRejectInterceptor extends HandlerInterceptorAdapter {

    @Autowired
    AuthRejectService authRejectServie;

    @Autowired
    RedisTemplate redisTemplate;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String token = request.getParameter("token");
        int count = authRejectServie.selectCount(new EntityWrapper<AuthReject>().eq("user_id", redisTemplate.opsForValue().get("access:token:" + token)).gt("unlock_time", DateUtil.getCurrentDate()));

        if (count > 0) {
            request.getRequestDispatcher("/errorPage5.json").forward(request, response);
            return false;
        }
        return true;
    }
}
